HackerRank Flatland Space Stations
提出
TLE
code: python
import math
import os
import random
import re
import sys
# Complete the flatlandSpaceStations function below.
def flatlandSpaceStations(n, c):
# Time Out
# ans = -1
# for i in range(n):
# tmp_res = float('inf')
# for j in c:
# res = abs(i - j)
# if (res < tmp_res):
# tmp_res = res
# if (tmp_res > ans):
# ans = tmp_res
# return ans
if __name__ == '__main__':
nm = input().split()
c = list(map(int, input().rstrip().split()))
result = flatlandSpaceStations(n, c)
fptr.write(str(result) + '\n')
fptr.close()
解答
code: python
import math
import os
import random
import re
import sys
# Complete the flatlandSpaceStations function below.
def flatlandSpaceStations(n, c):
answer = 0
c.sort()
for i in range(1, len(c)):
answer = max(answer, (ci-ci-1) // 2) answer = max(answer, c0, n-1 - c-1) return answer
if __name__ == '__main__':
nm = input().split()
c = list(map(int, input().rstrip().split()))
result = flatlandSpaceStations(n, c)
fptr.write(str(result) + '\n')
fptr.close()
メモ
https://www.youtube.com/watch?v=6d1ZBC9Dj1Q
提出
code: python
import math
import os
import random
import re
import sys
# Complete the flatlandSpaceStations function below.
def flatlandSpaceStations(n, c):
d = []
stations = sorted(c)
tmp = stations.pop(0)
for i in range(n):
tmpd = abs(tmp - i)
nexd = abs(stations0 - i) if len(stations) > 0 else pow(10, 10) if nexd < tmpd and len(stations) > 0:
tmp = stations.pop(0)
d.append(nexd)
else:
d.append(tmpd)
return max(d)
if __name__ == '__main__':
nm = input().split()
c = list(map(int, input().rstrip().split()))
result = flatlandSpaceStations(n, c)
fptr.write(str(result) + '\n')
fptr.close()